#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2023-09-21 21:03
# @Author : hard work
# @File    : main_compare_cb_field.py
# @Description :
import os
import sys
from utils.my_loger import Log
from parse_pkt_base import Parse_pcap_base
mylog = Log()


class Parse_cb_field(Parse_pcap_base):

    # def __init__(self, file):
    #     self.mylog = Log()
    #     self.__dst_mac_lst = []
    #     self.__src_mac_lst = []
    #     self.__vid_lst = []
    #     self.__priority_lst = []
    #     self.__seq_lst = []
    #     self.__pkt_len_lst = []
    #     self.__pkt_len_cnt = 0
    #     self.__stc_sig_lst = []
    #     self.__file = file
    #     self.parse_pcap_utils()
    #
    # def parse_pcap_utils(self):
    #     try:
    #         __pcap = pyshark.FileCapture(self.__file)
    #         for pkt in __pcap:
    #             __dst_mac = str(pkt.eth.dst)
    #             self.__dst_mac_lst.append(__dst_mac)
    #
    #             __src_mac = str(pkt.eth.src)
    #             self.__src_mac_lst.append(__src_mac)
    #
    #             __vid = str(pkt.vlan.id)
    #             self.__vid_lst.append(__vid)
    #
    #             __priority = str(pkt.vlan.priority)
    #             self.__priority_lst.append(__priority)
    #
    #             __seq = str(pkt.IEEE8021CB.SEQ)
    #             hex_2_int__seq = int(__seq, 16)
    #             self.__seq_lst.append(hex_2_int__seq)
    #
    #             __pkt_len = str(pkt.length)
    #             self.__pkt_len_lst.append(__pkt_len)
    #
    #             __stc_trans_2_int = int(pkt.STCSIG.seqnum)
    #             self.__stc_sig_lst.append(__stc_trans_2_int)
    #
    #             self.__pkt_len_cnt += 1
    #
    #     except RuntimeError as e:
    #         self.mylog.ERROR_LOG("pyshark运行时发生错误", e)
    #
    #     else:
    #         __pcap.close()
    def __init__(self, file):
        super().__init__(file)

    def parse_pkt_length(self):
        pkt_len = [pkt_len for pkt_len in self.pkt_len_lst]
        self.mylog.NOTICE_LOG("parse pkt_len: %s" % set(pkt_len))
        return self.pkt_len_lst

    def parse_src_mac(self):
        src = [src for src in self.src_mac_lst]
        self.mylog.NOTICE_LOG("parse src_mac: %s" % set(src))
        return self.src_mac_lst

    def parse_dst_mac(self):
        dst = [dst for dst in self.dst_mac_lst]
        self.mylog.NOTICE_LOG("parse dst_mac: %s " % set(dst))
        return self.dst_mac_lst

    def parse_vid(self):
        vid = [vid for vid in self.vid_lst]
        self.mylog.NOTICE_LOG("parse vid: %s" % set(vid))
        return self.vid_lst

    def parse_cos(self):
        cos = [cos for cos in self.priority_lst]
        self.mylog.NOTICE_LOG("parse cos: %s" % set(cos))
        return self.priority_lst

    def parse_seq(self):
        cnt = 0
        while cnt < len(self.seq_lst) - 1:
            if self.seq_lst[cnt + 1] - 1 == self.seq_lst[cnt]:
                self.mylog.NOTICE_LOG("第%s比较 %s 减去1 == %s" % (cnt, self.seq_lst[cnt + 1], self.seq_lst[cnt]))
            else:
                self.mylog.WARNNING_LOG("第%s比较 %s 减去1 ！= %s" % (cnt, self.seq_lst[cnt + 1], self.seq_lst[cnt]))

            cnt += 1

        return self.seq_lst

    def parse_stc_num(self):
        cnt = 0
        while cnt < len(self.stc_sig_lst) - 1:
            if self.stc_sig_lst[cnt + 1] - 1 == self.stc_sig_lst[cnt]:
                self.mylog.NOTICE_LOG("第%s比较 %s 减去1 == %s " % (
                    cnt, self.stc_sig_lst[cnt + 1], self.stc_sig_lst[cnt]))
            else:
                self.mylog.WARNNING_LOG("第%s比较 %s 减去1 != %s " % (
                    cnt, self.stc_sig_lst[cnt + 1], self.stc_sig_lst[cnt]))
            cnt += 1

        return self.stc_sig_lst

    def parse_pkt_cnt(self):
        return self.pkt_len_cnt

    def parse_and_compare(self):
        return self.parse_src_mac(), self.parse_dst_mac(), self.parse_vid(), self.parse_cos(), self.parse_seq(), \
               self.parse_stc_num(), self.parse_pkt_length()

def main():
    # D:\cap\cb.pcap
    # D:\cap\cb_bak.pcap
    try:
        _select = input("[A:解析一个文件\nB:比较两个文件:\nC:quit?]\n>>> ")
        if "A" == _select or "a" == _select:
            file_a = input("请输文件的路径>>> ")
            if os.path.isfile(file_a.strip()):
                pcap = Parse_cb_field(file_a.strip())
                print("parse file %s " % file_a)
                pcap.parse_and_compare()
            else:
                raise 'file %s not found' % file_a
        elif "B" == _select or "b" == _select:
            print("please input need compare pcap path")
            file_a = input("请输入第一个文件的路径>>> ")
            file_b = input("请输入第二个文件的路径>>> ")

            if os.path.isfile(file_b.strip()) or os.path.isfile(file_a.strip()):
                parse_2 = Parse_cb_field(file_b)
                parse_1 = Parse_cb_field(file_a)
                if parse_1.parse_dst_mac() == parse_2.parse_dst_mac():
                    mylog.NOTICE_LOG("%s dst mac equal %s dst mac" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s dst mac not equal %s dst mac" % (file_a, file_b))
                if parse_2.parse_src_mac() == parse_1.parse_src_mac():
                    mylog.NOTICE_LOG("%s src mac equal %s src mac" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s src mac not equal %s src mac" % (file_a, file_b))

                if parse_2.parse_vid() == parse_1.parse_vid():
                    mylog.NOTICE_LOG("%s vid equal %s vid" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s vid not equal %s vid" % (file_a, file_b))

                if parse_2.parse_cos() == parse_1.parse_cos():
                    mylog.NOTICE_LOG("%s cos equal %s cos" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s cos not equal %s cos" % (file_a, file_b))

                if parse_2.parse_seq() == parse_1.parse_seq():
                    mylog.NOTICE_LOG("%s 802.1cb seq equal %s 802.1cb seq" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s 802.1cb seq not equal %s 802.1cb seq" % (file_a, file_b))
                if parse_2.parse_pkt_length() == parse_1.parse_pkt_length():
                    mylog.NOTICE_LOG("%s all pkt len equal %s all pkt len" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s all pkt len not equal %s all pkt len" % (file_a, file_b))

                if parse_2.parse_pkt_cnt() == parse_1.parse_pkt_cnt():
                    mylog.NOTICE_LOG("%s all pkt cnt equal %s all pkt cnt" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s all pkt cnt not equal %s all pkt cnt" % (file_a, file_b))

                if parse_2.parse_stc_num() == parse_1.parse_stc_num():
                    mylog.NOTICE_LOG("%s stc_num equal %s stc_num" % (file_a, file_b))
                else:
                    mylog.ERROR_LOG("%s stc_num not equal %s stc_num" % (file_a, file_b))
            else:
                raise 'file %s not found' % file_a
        elif "C" == _select or "c" == _select:
            sys.exit(1)
    except FileExistsError as e:
        print("文件不存在", e)
    except SystemExit as e:
        print("quit", e)


if __name__ == '__main__':
    main()